package com.ljc.service;

import com.github.pagehelper.PageHelper;
import com.ljc.dao.UserDao;
import com.ljc.entity.CollegeUsersSum;
import com.ljc.entity.User;
import com.ljc.vo.UserSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findByDeanId(int deanId) {
        return userDao.findByDeanId(deanId);
    }

    @Override
    public User login(User user) {
        User userDB = userDao.login(user);
        if (user!=null){
            if(userDB.getStatus()==0)
                throw new RuntimeException("该用户已经被管理禁用");
            if (userDB.getDeleted()==1)
                throw new RuntimeException("该用户已经被管理删除");
            return userDB;
        }
        throw new RuntimeException("登录失败");
    }

    @Override
    public List<CollegeUsersSum> getCollegeUserSum(int deanId) {
        return userDao.getCollegeUserSum(deanId);
    }

    @Override
    public List<User> searchUser(UserSearch userSearch) {
        return userDao.searchUser(userSearch);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public void addUser(User user) {
        User userDB = userDao.getUserByName(user);
        if (userDB==null){
            userDao.addUser(user);
        }else{
            throw  new RuntimeException("该用户已经被注册");
        }
    }

    @Override
    public User getOne(int userId) {
        return userDao.getOne(userId);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public void deleteUser(int userId) {
        userDao.deleteUser(userId);
    }

    @Override
    public String getCollegeNameByUserId(int id) {
        return userDao.getCollegeNameByUserId(id);
    }

}
